//
//  ShareDetail.swift
//  Share
//
//  Created by MBA on 2020/10/4.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI

struct ShareDetail: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    @ObservedObject var shareStore:ShareDetailStore
    
    @State var editStore:Store?
    @State var isNavStore = false
    @State var loading = false
    
    
    func updatePrice()  {
        shareStore.updatePrice()
    }
    
    var body: some View {
        
        
        
        ScrollView{
            Section(){
                NavigationLink(destination: ShareEdit(shareStore: shareStore) ){
                    HStack{
                        
                        VStack{
                            Progress(percent:shareStore.percent, colors: [Color("danger"),Color("card1")])
                                .frame(width: 80, height: 80, alignment: .center)
                            Text("持仓收益：¥\((shareStore.shizhi-shareStore.share.getAllPrice()), specifier: "%0.2f")")
                                .font(.system(size: 12))
                                .padding(.vertical)
                        }
                        Spacer()
                        
                        VStack(alignment: .leading) {
                            Button(action: {updatePrice()}, label: {
                                HStack {
                                    Text("现价：¥\(Float(shareStore.share.price) ?? 0.00, specifier: "%0.3f")")
                                        .font(.system(size: 14))
                                    Image(systemName: "goforward")
                                        .font(.system(size: 12))
                                        .frame(width: 16, height: 16, alignment: .center)
                                }
                                .foregroundColor(Color("danger"))
                            })
                            VStack{
                                
                                Text("持有：\(shareStore.share.getAllNum())")
                                Text("成本：¥\(shareStore.share.getAllPrice(), specifier: "%0.3f")")
                                Text(String(format:"市值：¥%0.3f", arguments: [shareStore.shizhi]))
                            }
                        }
                        .foregroundColor(.grey_2)
                        .font(.system(size: 12))
                        
                    }
                    //                        .frame(height: 135, alignment: .center)
                    .padding(.all)
                }
            }
            
            
            let buyarr = shareStore.share.fitteInstores()
            if buyarr.count>0{
                Section(header:Text("委买")
                ){
                    let rows: [GridItem] =
                        Array(repeating: .init(.fixed(20)), count: 1)
                    ScrollView(.horizontal) {
                        LazyHGrid(rows: rows, alignment: .top) {
                            ForEach(buyarr) { item in
                                PreTransactionBuyCell(store: item.store, share: item.share)
                            }
                        }
                    }
                    .frame(height: 95, alignment: .center)
                    .padding(.all)
                }
                
                
                
            }
            
            
            let sellarr = shareStore.share.fitteOutstores()
            if sellarr.count>0{
                Section(header:Text("委卖")){
                    let rows: [GridItem] =
                        Array(repeating: .init(.fixed(20)), count: 1)
                    ScrollView(.horizontal) {
                        LazyHGrid(rows: rows, alignment: .top) {
                            ForEach(sellarr) { item in
                                PreTransactionSellerCell(store: item.store, share: item.share)
                            }
                        }
                    }
                    .frame(height: 95, alignment: .center)
                    .padding(.all)
                    
                }
            }
            
            let storearr = shareStore.share.stores
            if storearr.count>0{
                Section(header:Text("持仓")){
                    let rows: [GridItem] =
                        Array(repeating: .init(.fixed(20)), count: 1)
                    ScrollView(.horizontal) {
                        LazyHGrid(rows: rows, alignment: .top) {
                            ForEach(storearr.indices ,id:\.self){ index in
                                
                                
                                SroreCell(store: storearr[index], price: Float(shareStore.share.price) ?? 0.0)
                                    .onTapGesture(count: 1, perform: {
                                        editStore = storearr[index]
                                        self.isNavStore = true
                                    })
                                
                                
                            }
                        }
                    }
                    .frame(height: 105, alignment: .center)
                    .padding(.all)
                    
                }
            }
            
            let outarr = shareStore.share.outstores
            if outarr.count>0{
                Section(header:Text("已卖出")){
                    let rows: [GridItem] =
                        Array(repeating: .init(.fixed(20)), count: 1)
                    ScrollView(.horizontal) {
                        LazyHGrid(rows: rows, alignment: .top) {
                            ForEach(outarr.indices ,id:\.self){ index in
                                
                                StoreOutCell(store: outarr[index], price: Float(shareStore.share.price) ?? 0.0)
                                    .onTapGesture(count: 1, perform: {
                                        editStore = outarr[index]
                                        self.isNavStore = true
                                    })
                                
                                
                            }
                        }
                    }
                    .frame(height: 105, alignment: .center)
                    .padding(.all)
                    
                }
            }
            
            
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    
                    HStack {
                        Text("添加交易")
                            .padding(.leading)
                        
                        Image(systemName: "rectangle.and.pencil.and.ellipsis")
                            .foregroundColor(Color("lable1"))
                            .frame(width: 44,height: 44, alignment: .center)
                    }
                    .background(Color("background3"))
                    .shadow(color: Color("shadow"), radius: 4, x: 4, y: 4)
                    .onTapGesture(count: 1, perform: {
                        self.editStore = Store()
                        self.isNavStore = true
                    })
                    
                    
                    
                }
                .padding(.all)
                
                if shareStore.loading{
                    LoadingView()
                        .background(Color.black.opacity(0.3))
                        .edgesIgnoringSafeArea(.all)
                }
            }
            
            .onAppear(perform: {
                shareStore.updatePrice()
            })
        }
        .sheet(isPresented: $isNavStore, content: {
            StoreEdit(pageStore: StoreEditStore(id: editStore?.id ?? 0, shareId: shareStore.share.id))
        })
        
        .navigationBarTitle("\(shareStore.share.name)",displayMode: .inline)
        
    }
}

struct ShareDetail_Previews: PreviewProvider {
    static var previews: some View {
        ShareDetail(shareStore: ShareDetailStore(id: 1))
            .preferredColorScheme(.light)
        
    }
}



